<?php   if (!defined('BASEPATH')) exit('No direct script access allowed');
	
	class Tours extends MX_Controller {
		
		var $settings = array();
		var $template = array();
		
		function __construct()
		{
			parent::__construct();
			//$this->output->enable_profiler(true);
			$this->template['module'] = "tours";

			$this->settings = isset($this->system->tours_settings) ? unserialize($this->system->tours_settings) : array();

			$this->load->model('tours_model', 'tours');
			//$this->lang = $this->session->userdata('lang');
		}
		
		function read($uri = null)
		{
			if (is_null($uri))
			{
				redirect('tours/list');
			}
			else
			{
				if($tours = $this->tours->get_tours($uri))
				{
					//pagination for comments
					
					//debug
					
					//hits
					if ($this->session->userdata('tours'.$tours['id']) != $tours['id'])
					{
						$this->session->set_userdata('tours'.$tours['id'], $tours['id']);
						$this->db->where('id', $tours['id']);
					    $this->db->set('hit', 'hit+1', FALSE);
						$this->db->update('tours');
						$this->cache->remove('tours'.$this->user->lang, 'tours');
					}
					
					//var_dump($rows->result_array());
					
					$this->template['title'] = $tours['title'];
					$this->template['tours'] = $tours;
					$this->template['settings'] = $this->settings;
					$this->layout->load($this->template, 'read');
				
				}
				else
				{
					$this->template['title'] = __("No tours found", $this->template['module']);
					$this->layout->load($this->template, '404');
				}
			}
		}
		
		
		function index($start = 0)
		{
			$per_page = 20;
			
			$params['limit'] = $per_page;
			$params['start'] = $start;
			$params['where'] = array('tours.lang' => $this->user->lang);
			
			
			$this->load->library('pagination');
			
			$config['uri_segment'] = 3;
			$config['first_link'] = __('First', 'tours');
			$config['last_link'] = __('Last', 'tours');
			$config['base_url'] = site_url('tours/list');
			$config['total_rows'] = $this->tours->get_total_published($params);
			$config['per_page'] = $per_page; 	
			$this->pagination->initialize($config); 

			$this->template['rows'] = $this->tours->get_list($params);
			$this->template['start'] = $start;
			$this->template['total_rows'] = $config['total_rows'];
			$this->template['pager'] = $this->pagination->create_links();		

			$this->template['title'] = __("All tours", $this->template['module']);
			
			$this->layout->load($this->template, 'index');
		}
		
		
		function cat ()
		{
			$uri = $this->uri->segment(3);
			$start = $this->uri->segment(4);
			$per_page = 20;
			
			$params['limit'] = $per_page;
			$params['start'] = $start;
			if(is_null($uri))
			{
				$params['where'] = array('tours.lang' => $this->user->lang, 'cat' => 0);
				$this->template['category'] = array('title' => __("No category", "tours"));
				$config['uri_segment'] = 3;
				$config['base_url'] = site_url('tours/cat');
			}
			else
			{
				$cat = $this->tours->get_cat(array('uri' => $uri, 'lang' => $this->user->lang));

				$params['where'] = array('tours.lang' => $this->user->lang, 'cat' => $cat['id']);
				$this->template['category'] = $cat;
				$config['uri_segment'] = 4;
				$config['base_url'] = site_url('tours/cat/' . $uri);
			}
			
			
			$this->load->library('pagination');
			
			$config['first_link'] = __('First', 'tours');
			$config['last_link'] = __('Last', 'tours');
			$config['total_rows'] = $this->tours;
			$config['per_page'] = $per_page; 	
			$this->pagination->initialize($config); 

			$this->template['rows'] = $this->tours->get_list($params);

			$this->template['start'] = $start;
			$this->template['total_rows'] = $config['total_rows'];
			$this->template['pager'] = $this->pagination->create_links();
			if(isset($cat))
			{
				$this->template['title'] = sprintf(__("tours in %s", $this->template['module']), $cat['title']);
			}
			else
			{
				$this->template['title'] = __("Other tours", $this->template['module']);			
			}

			
			$this->layout->load($this->template, 'index');
			
		}
		
	
	}

?>